package ua.org.plast.mobileApp.navigation;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.*;

/**
 * Created with IntelliJ IDEA.
 * User: Virus1908
 * Date: 08.09.13
 * Time: 21:46
 */
public class NavigationDrawerListView extends ListView {
    private NavigationAdapter.Switcher mSwitcher;

    public NavigationDrawerListView(Context context) {
        this(context, null);
    }

    public NavigationDrawerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMe();
    }

    public NavigationDrawerListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initMe();
    }

    private void initMe() {
        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                NavigationItem item = (NavigationItem) getItemAtPosition(i);
                if (item.hasSubItems()) {
                    NavigationAdapter.ViewHolder holder = (NavigationAdapter.ViewHolder) view.getTag();
                    if (item.isOpen) {
                        holder.subViews.setVisibility(GONE);
                        item.isOpen = false;
                    } else {
                        holder.subViews.setVisibility(VISIBLE);
                        item.isOpen = true;
                    }
                    return;
                }
                if(mSwitcher != null){
                    mSwitcher.switchTo(item.launch);
                }
            }
        });
    }

    public void setSwitcher(NavigationAdapter.Switcher switcher) {
        this.mSwitcher = switcher;
    }
}
